<?php
namespace Attendance\Controller;
use Common\Controller\AdminbaseController;
use Home\Service\ClockTimeService;
use Home\Service\AccountService;
use Home\Service\ResetDataService;
// +----------------------------------------------------------------------
// | Date:2017年1月17日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class ConfigController extends AdminbaseController{
    
    
    public function _initialize(){
        parent::_initialize();
        $this->model = D('Options');
    }
    
    
    public function index(){
        $list = $this->model->where(['option_name'=>'loader_box'])->getField('option_value');
        if ($list) $list = json_decode($list,true);
        $this->assign('list',$list);
        
        $this->display();
    }
    
    
    
    //添加配置
    public function add(){
        if (IS_POST){
            $post = I('post');
            $cfg[$post['key']] =$post['name'];
            
            $key_attcfg = "att_cfg_".$post['key'];
            $key_box = "loader_box";
            
            $isHasKey = $ret = $this->model->where(['option_name'=>$key_attcfg])->getField('option_value');
            if ($isHasKey) $this->error("{$post['key']}配置已经存在，请勿重复添加！！");
            
            $ret = $this->model->where(['option_name'=>$key_box])->getField('option_value');
            if ($ret) {
                $retAry = json_decode($ret,true);
                $cfg = $retAry+$cfg;
                $update = $this->model->where(['option_name'=>$key_box])->setField('option_value',json_encode($cfg,JSON_UNESCAPED_UNICODE));
            }else{
                $update = $this->model->add([
                    'option_name'=>$key_box,
                    'option_value'=>json_encode($cfg,JSON_UNESCAPED_UNICODE),
                ]);
            }
            
            if ($update){
                $default = ["wait_time"=>"60","last_id"=>"1","limit"=>30];
                $createKey = $this->model->add([
                    'option_name'=>$key_attcfg,
                    'option_value'=>json_encode($default,JSON_UNESCAPED_UNICODE),
                    'remark'=>"[{$post['name']}]数据读取配置"
                ]);
                if ($createKey){
                    $this->success('添加成功！');
                }else{
                    $this->error('添加失败！');
                }
            }
            
        }else{
            
            $this->display();
        }
        
    }
    
    //编辑配置
    public function edit(){

        $key = I('key');
        $opKey = 'att_cfg_'.$key;

        if (IS_POST){
            $post = I('post');
            $ret = $this->model->setOption($opKey,$post);
            if (!$ret) $this->error('操作失败，网络异常！');
            $this->success('操作完成！');
            exit();
        }else{
            
            $info = $this->model->where(['option_name'=>$opKey])->getField('option_value');
            if ($info) $info = json_decode($info,true);
            $this->assign('info',$info);
            $this->display();
        }
        
        
    }
    
    
    public function delete(){
        $key = I('key');
        $opkey = 'att_cfg_'.$key;
        $loaderKey = 'loader_box';
        $loader = $this->model->where(['option_name'=>$loaderKey])->getField('option_value');
        if ($loader){
            $cfgLoader = json_decode($loader,true);
            unset($cfgLoader[$key]);
            $de_loader = $this->model->where(['option_name'=>$loaderKey])->setField('option_value',json_encode($cfgLoader,JSON_UNESCAPED_UNICODE));
            $de_att = $this->model->where(['option_name'=>$opkey])->delete();
        }

        if ($de_loader) {
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！！');
            
        }
    }
    
    private function setOption($opt_key,$isCache){
        $post = I('post');
        if (IS_POST) {
            $ret = $this->model->setOption($opt_key,$post,$isCache);
            if (!$ret) $this->error('操作失败，网络异常！');
            $this->success('操作完成！');
            exit();
        
        }
        
        $info =$this->model->getOption($opt_key,$isCache);
        $this->assign('info',$info);
    }
    
    /**
     * 重置日常考勤数据
     * 
     * date:2017年6月14日
     * author: EK_熊
     */
    public function flash_data(){
        $this->display();
    }
    
    /**
     * 刷新重置个人日常数据
     * 1.根据条件（姓名和日期，先删除旧数据，再重新拉取流水数据再次生成日常数据）
     * 2.删除用户相关的 申请记录，审核人记录
     * date:2017年3月10日
     * author: EK_熊
     */
    public function flash_user(){
        $name = trim(I('name'));
        $date_s = I('date_s');
        $days = I('days');
        $day_cur = date("Y-m-d",strtotime($date_s)+86400*$days);
        $resetService  = new ResetDataService();
        $ret = $resetService->reset_user_attday($name, $day_cur);
        
        if(!$ret) {
            $this->error($resetService->error);
        }
       $this->ajaxReturn(['msg'=>"[{$day_cur}]重载完成！！"]);
    }
    
    /**
     * 重刷数据，申请数据重刷
     * 
     * date:2017年5月2日
     * author: EK_熊
     */
    public function flash_user_apply(){
        $name = trim(I('name'));
        $date_s = I('date_s');
        $days = I('days');
        $date_s = date("Y-m-d",strtotime($date_s)+86400*$days);
        $deal_id = I('deal_id');//处理完毕的申请数据id
        $deal_id = $deal_id ? explode(',',$deal_id) : [];
        $resetService = new ResetDataService();
        $data = $resetService->reset_user_apply($name, $date_s,$deal_id);
        
        $this->ajaxReturn($data);
    }
    
    
    /**
     * 基础设置,数据写入缓存
     * 
     * date:2017年2月27日
     * author: EK_熊
     */
    public function base(){
        
       $this->setOption('att_base',true);
       $AccountService = D('Home/Account','Service');
       $hr = $AccountService->get_all_user_baseinfo('sn,ep_name,wx_openid',['dep_id'=>25,'status'=>1]);
       $admin = $AccountService->get_all_user_baseinfo('sn,ep_name,wx_openid',['dep_id'=>38,'status'=>1]);
       
       $this->assign('admin',$admin); 
       $this->assign('hr',$hr); 
       $this->display();
    }
    
    /**
     * 数据导入设置
     * 
     * date:2017年2月27日
     * author: EK_熊
     */
    public function loader(){
        $this->setOption('dev_loader');
        $this->display();
    }
    

    
    
}